<?php

/**
 * message actions.
 *
 * @package    namasce
 * @subpackage message
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class messageActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfWebRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }

 /**
  * Executes view action
  *
  * @param sfWebRequest $request A request object
  */
  public function executeView(sfWebRequest $request)
  {
    if ($request->isMethod('post') )
    {
      $msgId = $this->getUser()->getFlash('message_id');

      $comment = new MessageComment();
      $comment->content = $request->getParameter('comment');
      $comment->message_id = $msgId;
      $comment->user_id = myUser::id();

      if ($comment->isValid() && trim($comment->content) != '')
      {
        $comment->save();
      }

      $this->redirect('@view_msg?id=' . $this->getUser()->getFlash('message_slug'));
    }

    $this->message = MessageTable::findOneBySlug($request->getParameter('id'));
    $this->getUser()->setFlash('message_id', $this->message->id);
    $this->getUser()->setFlash('message_slug', $this->message->plate_message);
  }

 /**
  * Executes compose action
  *
  * @param sfWebRequest $request A request object
  */
  public function executeCompose(sfWebRequest $request)
  {
    $message = $request->getParameter('message');
    $plateNo = Message::preparePlate($request->getParameter('plate', isset($message['recipient_plate']) ? $message['recipient_plate'] : null));
    $message = trim($message['body']);

    $this->form = new MessageForm();

    if ($request->isMethod('post'))
    {
      $message = array(
        'user_id'         => myUser::id(),
        'recipient_plate' => $plateNo,
        'body'            => $message,
      );
      $this->form->bind($message);
      if ($this->form->isValid() && $plateNo && $message)
      {
        $this->form->save();
        $this->redirect('@list_by_plate?id=' . $plateNo);
      }
    }

    if ($plateNo)
    {
      $this->tracking = PlateTrackerTable::isTracked($plateNo);
    }
  }
}
